package br.com.xml;

import java.io.File;

import br.com.util.Escritor;

import com.hp.hpl.jena.graph.Triple;
import com.thoughtworks.xstream.XStream;

public class XML {
	XStream xstream = null;
	
	private void config() {
		xstream = new XStream();
		xstream.alias("mashup", Object[].class);
		xstream.alias("recuperar", Recuperar.class);
		//xstream.useAttributeFor(Recuperar.class, "uri");
		//xstream.useAttributeFor(Recuperar.class, "endpoint");
	}
	
	public void generateSample(String filePath) {
		if (xstream == null) config();
		
		//xstream.fromXML(filePath);
		String endpoint = "http://sparql.data.southampton.ac.uk/";
		String uri = "http://creativecommons.org/licenses/by/3.0/";
		
		Object[] operations = new Object[1];
		operations[0] = new Recuperar(endpoint, uri);
		
		Escritor e = new Escritor();
		e.abreArquivo(filePath, true);
		e.gravaArquivo(new String[] { xstream.toXML(operations) });
		e.fechaArquivo();
	}
	
	public Object[] parse(String filePath) {
		if (xstream == null) config();
		
		return (Object[]) xstream.fromXML(new File(filePath));
	}
	
	public static void main(String[] args) {
		/*
		new XML().generateSample("j:/script.xml");
		*/
		for(Object o : new XML().parse("j:/script.xml")) {
			if (o instanceof Recuperar) {
				Recuperar e = new Recuperar();
				System.out.println("Endpoint: " + e.getEndpoint());
				System.out.println("URI: " + e.getUri());
				System.out.println("===");
				Triple[] ts = e.exec();
				for(Triple t : ts) {
					System.out.println("Subject: " + t.getSubject());
					System.out.println("Predicate: " + t.getPredicate());
					System.out.println("Object: " + t.getObject());
					System.out.println("---");					
				}
			}
		}
		
	}
}
